/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package laba5;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import java.util.TreeSet;

/**
 *
 * @author Simplevolk
 */
public class lab {



    
    /**
     * @param args the command line arguments
     */
   private static ArrayList<Person> peoples;

   // Для шаблона Singletone статическая переменная
    private static lab instance;

private lab() {

        loadPeople();
    }
    // метод getInstance - проверяtт, инициализирована ли статическая
    // переменная (в случае надобности делает это) и возвращает ее
    public static synchronized lab getInstance() {
        if (instance == null) {
            instance = new lab();
        }
        return instance;
    }


    public void loadPeople()
    {
         if (peoples == null) {
            // Мы используем коллекцию, которая автоматически сортирует свои элементы
            peoples = new ArrayList<Person>();
        } else {
            peoples.clear();
        }
/*
    Person pp=new Person();
    pp.setSurName("ff");
    pp.setFirstName("sss");
    pp.setPatronymic("sssss");
    pp.setDateOfBirth("sswret");
    pp.setSex("male");
    pp.setTelefone("myphone");
    pp.setPlace("ssss");
    peoples.add(pp);
 *
 */
    }
    public ArrayList<Person> getPeoples()
    {
    return peoples;
    }
    public void insertPeople(Person people)
    {
    peoples.add(people);
    }
    public void deletePeople(Person people)

    {
    peoples.remove(people);
    }
      public static void printString(Object s) {
        //System.out.println(s.toString());
        try {
            System.out.println(new String(s.toString().getBytes("windows-1251"), "windows-1252"));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args) {
     
        
        // TODO code application logic here
      peoples = new ArrayList<Person>(10);
      int flag=0;
     lab ms=lab.getInstance();
     
    Person p=null;
    
     Scanner input= new Scanner(System.in);
     String str;
     lab l=new lab();
    System.out.println(l.getPeoples());

      while(flag==0)
      {
       p=new Person();

    //  System.out.println("Vvedite ID: ");
    //  str=input.next();
    //  p.setPeopleId(Integer.parseInt(str));

       System.out.println("Vvedite Familiy:");
        
        str=input.next();
        p.setSurName(str);
        
        System.out.println("Vvedite Imia: ");
       
        str=input.next();
        p.setFirstName(str);

        System.out.println("Vvedite Otchestvo: ");
        str=input.next();
        p.setPatronymic(str);
        
        System.out.println("Vvedite pol: ");
        str=input.next();
        p.setSex(str);

        System.out.println("Vvedite Daty:");
        str=input.next();
        p.setDateOfBirth(str);

        System.out.println("Vvedite telefon: ");
        str=input.next();
        p.setTelefone(str);

        System.out.println("Vvedite Mesto: ");
        str=input.next();
        p.setPlace(str);
        ms.insertPeople(p);
//peoples.add(p);
       
       
        System.out.println("Zakonchit vvod? Yes=1;No=0");
        
         str=input.next();

         flag=Integer.parseInt(str);

      try {
            System.setOut(new PrintStream("out.txt"));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return;
        }

Collection<Person> allPeoples=ms.getPeoples();
     for(Person pp:allPeoples)
       {
       printString(""+pp.toString());
       }     
        }

    
      
    System.out.println();


    }
}
